<?php
class BookController extends Bib_Controller_Action {
	public function orderAction() 
	{
		$isbn = $this->_getParam('isbn');	
		$edition = EditionManager::getInstance()->getByIsbn($isbn);
		
		if ($edition->dostepne) {
			$copies = $edition->getAvailableCopies();
			$copy = $copies[0];
		} else {
			$copies = $edition->getCopies();
			$copy = $copies[0];
		}
		
		$lend = new Lend();
		$lend->idEgemplarza = $copy->id;
		$lend->typ = Lend::TYPE_ORDERED;
		$lend->email = $this->user->email;
		LendManager::getInstance()->save($lend);
		
		$this->view->infoBox();
		$this->view->message = "Książka zamówiona.";
		$this->view->redirect = 'javascript:history.back()';
	}
	
	public function ordersListAction() 
	{
		$manager = LendManager::getInstance();
		
        $this->view->zamowione = $manager->getByType(Lend::TYPE_ORDERED);
        $this->view->wypozyczone = $manager->getByType(Lend::TYPE_LEND);
        $this->view->oczekujace = $manager->getByType(Lend::TYPE_WAITING);
	}
	
	public function userOrdersAction() 
	{
		$email = $this->_getParam('email');	

		$user = UserManager::getInstance()->getByEmail($email);
		$this->view->user = $user;
		
        $this->view->zamowione = $user->getLendsByType(Lend::TYPE_ORDERED);
        $this->view->wypozyczone = $user->getLendsByType(Lend::TYPE_LEND);
        $this->view->oczekujace = $user->getLendsByType(Lend::TYPE_WAITING);
        $this->view->zwrocone = $user->getLendsByType(Lend::TYPE_RETURNED);  
	}

	public function cancelOrderAction() 
	{
		$id = $this->_getParam('id');	
		$lend = LendManager::getInstance()->getById($id);
		
		if ($lend->typ == Lend::TYPE_ORDERED) {
			LendManager::getInstance()->delete($lend);
		}
		
		$this->view->infoBox();
		$this->view->message = "Zamównieie anulowane.";
		$this->view->redirect = 'javascript:history.back()';
	}

	public function setOrderStatusAction() 
	{
		$id = $this->_getParam('id');
		$type = $this->_getParam('type');	
		$lend = LendManager::getInstance()->getById($id);
		
		$lend->typ = $type;
		
		LendManager::getInstance()->save($lend);
		
		$this->view->infoBox();
		$this->view->message = "Status zamówniea został zmieniony.";
		$this->view->redirect = 'javascript:history.back()';
	}
	
	public function searchAction()
	{
		$form = new Form_Book_Search();
		
		if ($this->_request->isPost() && $form->isValid($this->_getAllParams())) {
			$editions = EditionManager::getInstance()->search($form->getValue('title'), $form->getValue('author'));
			$this->view->editions = $editions;
		}
		$this->view->form = $form;	
	}
	
	public function newAction()
	{
		$form = new Form_Book_Edition();
		
		if ($this->_request->isPost() && $form->isValid($this->_getAllParams())) {
			$edition = new Edition($form->getValues());
			if ( ! $edition->idKsiazki) {
				$book = new Book();
				BookManager::getInstance()->save($book);
				$edition->idKsiazki = $book->id;
			}
			
			$catId = $edition->catId;
			unset($edition->catId);
			
			EditionManager::getInstance()->save($edition);
			
			$copy = new Copy();
			$copy->isbn = $edition->isbn;
			CopyManager::getInstance()->save($copy);
			
			if ( $catId) {
				$cat = CategoryManager::getInstance()->getById($catId);
				$cat->addBook($edition->getBook());
			}
			
			$this->view->infoBox();
			$this->view->message = "Książka zapisana.";
			$this->view->redirect = 'javascript:history.go(-2)';
		} else {
			$catId = $this->_getParam('catId');
			$form->catId->setValue($catId);
			$bookId = $this->_getParam('bookId');
			$form->idKsiazki->setValue($bookId);
		}
		
		$this->view->form = $form;
	}
	
	public function editAction()
	{
		$form = new Form_Book_Edition();
		
		if ($this->_request->isPost() && $form->isValid($this->_getAllParams())) {
			$edition = new Edition($form->getValues());
			$edition->setSaved();
			
			unset($edition->catId);
			
			EditionManager::getInstance()->save($edition);
			
			$this->view->infoBox();
			$this->view->message = "Książka zapisana.";
			$this->view->redirect = 'javascript:history.go(-2)';
		} else {
			$isbn = $this->_getParam('isbn');
			$edition = EditionManager::getInstance()->getByIsbn($isbn);
			$form->populate($edition->getProperties());
		}
		
		$this->view->form = $form;
	}

	public function newCopyAction() 
	{
		$isbn = $this->_getParam('isbn');
		
		$copy = new Copy();
		$copy->isbn = $isbn;
		CopyManager::getInstance()->save($copy);
		
		$this->view->infoBox();
		$this->view->message = "Nowy egzemplarz dodany.";
		$this->view->redirect = 'javascript:history.back()';
	}

	public function deleteAction() 
	{
		$isbn = $this->_getParam('isbn');
		
		$edition = EditionManager::getInstance()->getByIsbn($isbn);
		EditionManager::getInstance()->delete($edition);
		
		$this->view->infoBox();
		$this->view->message = "Egzemplarz usunięty.";
		$this->view->redirect = 'javascript:history.back()';
	}
	
}